package jdbc.dao.impl;

import jdbc.dao.UserDao;
import jdbc.entity.User;
import jdbc.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2023-07-04 16:41
 * Description：<描述>
 */
@Repository
@RequiredArgsConstructor
public class UserDaoImpl  implements UserDao {

    private final JdbcTemplate jt;

    @Override
    public void insert(User user) {
        String sql = "insert into t_user(username,password) values(?,?)";
        jt.update(sql, user.getUsername(), user.getPassword());
    }

    @Override
    public User selectByUsernameAndPassword(String username, String password) {
        String sql = "select id,username,password from t_user where username=? and password=?";
        List<User> list = jt.query(sql, new UserMapper(), username, password);
        return list.isEmpty() ? null : list.get(0);
    }
}
